#!/usr/bin/python

import sys
import commands
import socket

import xml.dom.minidom
from xml.dom.minidom import parseString, Node

def ShowTopology(rackNum):
    print '<?xml version=\"1.0\" ?>'
    print '<lunmgr_conf>'
    print '  <rack number="%d">' % rackNum

    (status, scli_output) = commands.getstatusoutput('/usr/local/bin/scli -t -x')

    dom = parseString(scli_output)
    xml_root = dom.firstChild

    for node in xml_root.childNodes:
        if node.nodeType != node.ELEMENT_NODE: continue
        if node.tagName != 'HBA': continue

        for nodeHba in node.childNodes:
            if nodeHba.nodeType != node.ELEMENT_NODE: continue
            if nodeHba.tagName == 'HBA':
                port = nodeHba.getAttribute('Port')
                wwnn = nodeHba.getAttribute('WWNN').replace('-', '')
                wwpn = nodeHba.getAttribute('WWPN').replace('-', '')

                print '    <port number="%s" wwnn="%s" wwpn="%s" src="%s">' \
                  % (port, wwnn, wwpn, socket.gethostname())

            else: # Target child
                elemWwnn = nodeHba.getElementsByTagName('WWNN')[0]
                wwnn = elemWwnn.firstChild.data.replace('-', '')
                elemWwpn = nodeHba.getElementsByTagName('WWPN')[0]
                wwnn = elemWwpn.firstChild.data.replace('-', '')
                elemInfo = nodeHba.getElementsByTagName('Info')[0]
                portid = elemInfo.getAttribute('PortID')

                print '      <disk_enclosure wwnn="%s" wwpn="%s" port="%s">' \
                      % (wwnn, wwpn, portid)

                # print lun info

#                print '        <lun id="%s">'
#                print '          <device>%s</device>'
#                print '          <mpath>%s</mpath>'
#                print '          <size>%s</size>'
#                print '          <mount_pt>%s</mount_pt>'
#                print '          <name>%s</name>'
#                print '        </lun>'

                print '      </disk_enclosure>'

        print '    </port>'

    print '  </rack>'
    print '</lunmgr_conf>'

if __name__ == '__main__':
    from optparse import OptionParser
    parser = OptionParser()
    parser.add_option("--rack", dest='rackNum',
                      help='rack number', type='int', default=1)
    (option, args) = parser.parse_args()

    ShowTopology(option.rackNum)
